﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TiecCuoi
{
    public partial class DoanhThu : Form
    {
        //Data base
        private SqlConnection _CNN;
        long TongDoangThu;
        public DoanhThu(SqlConnection cnn)
        {
            _CNN = cnn;
            InitializeComponent();
            TaoCompoBoxNam();
            
        }
        private void TaoCompoBox()
        {
            int month = 0;
            comboBox_Thang.Items.Clear();
            if(comboBox_Nam.SelectedItem.ToString() !=DateTime.Now.Year.ToString())
            {
                month = 12;
            }
            else
            {
                month = DateTime.Now.Month;
            }
            for (int i = 1; i <= month; i++)
            {
                comboBox_Thang.Items.Add(i.ToString());
            }

        }
        private void TaoCompoBoxNam()
        {
            int  year = DateTime.Now.Year;
            for (int i = 2000; i <= year; i++)
            {
                comboBox_Nam.Items.Add(i.ToString());
            }

        }

        private void comboBox_Thang_SelectedIndexChanged(object sender, EventArgs e)
        {
            L_viewKQ.Items.Clear();
            TinhTongDoanhThuThang();
            if(TongDoangThu == 0)
            {
                return;
            }
            string thang = comboBox_Thang.SelectedItem.ToString();
            ListViewItem item;
            bool bool_mau = false;
            string doanhthu;
            long doanhthungay = 0;
            try
            {
                string cmd = "select day(NgayThanhToan), Count(MaTiec),sum(TongTienHoaDon) from HoaDon  where month(NgayThanhToan) = @Thang group by day(NgayThanhToan)";
                SqlCommand command = new SqlCommand(cmd, _CNN);
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@Thang", thang);
                SqlDataReader read = command.ExecuteReader();
                while (read.Read())
                {
                    item = new ListViewItem(read[0].ToString() + "/" + thang + "/" + DateTime.Now.Year.ToString());
                    item.SubItems.Add(read[1].ToString());
                    doanhthu = read[2].ToString();
                    doanhthu = CatString(doanhthu);
                    doanhthungay = Convert.ToInt64(doanhthu);
                    doanhthu = TachSo(doanhthu);
                    item.SubItems.Add(doanhthu);
                    double tile = (doanhthungay*100)/TongDoangThu;
                    item.SubItems.Add(tile.ToString());
                    if (bool_mau == false)
                        item.BackColor = Color.White;
                    else
                        item.BackColor = Color.FromArgb(245, 204, 102);
                    bool_mau = !bool_mau;
                    L_viewKQ.Items.Add(item);

                }
                read.Close();
            }
            catch (Exception emon)
            {
                MessageBox.Show(emon.Message);
                return;
            }

 

             
        }
        string CatString(string str)
        {
            int n = str.Length;
            for (int i = 0; i < n; i++)
            {
                if (str[i] == '.')
                {
                    str = str.Substring(0, i);
                    return str;
                }

            }
            return str;
        }

        public string TachSo(string luong)
        {
            string txt, txt1;
            txt1 = luong.Replace(",", "");
            txt = "";
            int n = txt1.Length;
            int dem = 0;
            for (int i = n - 1; i >= 0; i--)
            {
                if (dem == 2 && i != 0)
                {
                    txt = "," + txt1.Substring(i, 1) + txt;
                    dem = 0;
                }
                else
                {
                    txt = txt1.Substring(i, 1) + txt;
                    dem += 1;
                }
            }
            luong = txt;
            return luong;
      
        }

        private void TinhTongDoanhThuThang()
        {
            string thang = comboBox_Thang.SelectedItem.ToString();
            string chuoi;
            TongDoangThu = 0;
            try
            {
                string cmd = "select sum(TongTienHoaDon) from HoaDon  where month(NgayThanhToan) = @Thang";
                SqlCommand command = new SqlCommand(cmd, _CNN);
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@Thang", thang);
                SqlDataReader read = command.ExecuteReader();
                while (read.Read())
                {
                    chuoi = read[0].ToString();
                    chuoi = CatString(chuoi);
                    if(chuoi !="")
                    {
                        TongDoangThu = Convert.ToInt64(chuoi);
                    }
                    else
                    {
                        TongDoangThu = 0;
                    }
                    chuoi = TachSo(chuoi);
                    TB_DoanhThu.Text =chuoi;

                }
                read.Close();
            }
            catch (Exception emon)
            {
                MessageBox.Show(emon.Message);
                return;
            }
        }

        private void comboBox_Nam_SelectedIndexChanged(object sender, EventArgs e)
        {
            TaoCompoBox();
        }
    }
}
